﻿using System;
using System.Collections.Generic;
using UnityEngine;

// Token: 0x0200000D RID: 13
public class DamagingObject : MonoBehaviour
{
	// Token: 0x06000038 RID: 56 RVA: 0x000078DF File Offset: 0x00005ADF
	private void Start()
	{
		this.wep = base.GetComponent<WeaponScript>();
		this.myCol = base.GetComponent<Collider2D>();
		this.myRb = base.GetComponent<Rigidbody2D>();
	}

	// Token: 0x06000039 RID: 57 RVA: 0x00007908 File Offset: 0x00005B08
	public void Unstab()
	{
		Object.Destroy(this.frictionJoint);
		Sound.Play(this.pullOutSounds[Random.Range(0, this.pullOutSounds.Count)], base.transform.position, false, true, 1f);
		this.stabbedCol = null;
		base.GetComponent<SpriteRenderer>().sortingOrder = 29;
		this.recc = null;
		this.stabbedRb = null;
	}

	// Token: 0x0600003A RID: 58 RVA: 0x0000797C File Offset: 0x00005B7C
	private void Update()
	{
		this.cooldown -= Time.deltaTime;
		this.stabCheckTime -= Time.deltaTime;
		this.infectionTime -= Time.deltaTime;
		if (this.frictionJoint && this.stabCheckTime < 0f)
		{
			this.stabCheckTime = 0.2f;
			ContactFilter2D contactFilter = default(ContactFilter2D);
			contactFilter.NoFilter();
			contactFilter.layerMask = LayerMask.GetMask(new string[]
			{
				"Grunt"
			});
			List<Collider2D> list = new List<Collider2D>();
			this.myCol.OverlapCollider(contactFilter, list);
			if (!this.stabbedCol || !list.Contains(this.stabbedCol))
			{
				this.Unstab();
				return;
			}
			if (this.recc)
			{
				this.recc.Damage(Mathf.Abs(this.myRb.velocity.magnitude - this.stabbedRb.velocity.magnitude));
				if (Mathf.Abs(this.myRb.velocity.magnitude - this.stabbedRb.velocity.magnitude) > 1f)
				{
					GameManager.main.SpawnDamageNum(base.transform.position, Mathf.Abs(this.myRb.velocity.magnitude - this.stabbedRb.velocity.magnitude), this.recc.body);
				}
			}
		}
	}

	// Token: 0x0600003B RID: 59 RVA: 0x00007B1A File Offset: 0x00005D1A
	private bool RGBequal(Color c1, Color c2)
	{
		return c1.r == c2.r && c1.g == c2.g && c1.b == c2.b;
	}

	// Token: 0x0600003C RID: 60 RVA: 0x00007B48 File Offset: 0x00005D48
	private void OnCollisionEnter2D(Collision2D col)
	{
		if ((!this.wep || this.wep.held || this.myRb.velocity.magnitude > 7f) && (!this.currentOwner || this.currentOwner.health > 0f) && !this.frictionJoint)
		{
			float num = Mathf.Abs(col.relativeVelocity.magnitude * Mathf.Abs(Vector3.Dot(col.relativeVelocity.normalized, col.GetContact(0).normal)));
			if (num > this.minDamageSpeed && this.cooldown < 0f)
			{
				float num2 = (num - this.minDamageSpeed) * this.damageSpeedMult + this.damageMin;
				num2 = Mathf.Clamp(num2, 0f, this.maxDamage);
				if (this.wep)
				{
					this.wep.durability -= num2 * this.wep.damageMult;
				}
				if (!this.soundOnlyOnFleshHit)
				{
					Sound.Play(this.hitSounds[Random.Range(0, this.hitSounds.Count)], base.transform.position, false, true, 1f);
				}
				WeaponScript weaponScript;
				if (this.wep && col.gameObject.TryGetComponent<WeaponScript>(out weaponScript))
				{
					if (this.wep.clashTime < 0f && weaponScript.clashTime < 0f)
					{
						this.wep.clashTime = 0.3f;
						weaponScript.clashTime = 0.3f;
						Sound.Play(this.wep.clashSounds[Random.Range(0, this.wep.clashSounds.Count)], col.GetContact(0).point, false, true, 1f);
					}
					DamagingObject damagingObject;
					if (this.currentOwner && weaponScript.TryGetComponent<DamagingObject>(out damagingObject) && damagingObject.currentOwner && damagingObject.currentOwner.perfectBlockTime > 0f && damagingObject.currentOwner.timeSinceLastAttacked > 0.2f && this.currentOwner.timeSinceLastAttacked < 0.22f)
					{
						if (num2 > 3f)
						{
							Sound.Play(Resources.Load<AudioClip>("Sounds/perfectBlock"), Vector2.zero, true, false, 0.5f);
							GameManager.main.SpawnPerfectBlock(col.GetContact(0).point);
							this.currentOwner.hitStun += Mathf.Clamp01(this.currentOwner.hitStunMult * 0.3f);
						}
						else
						{
							Sound.Play(Resources.Load<AudioClip>("Sounds/perfectBlock"), Vector2.zero, true, false, 0.2f);
						}
						if (this.currentOwner)
						{
							this.myRb.velocity = -this.myRb.velocity * 2f;
						}
					}
				}
				DamageReceiver damageReceiver;
				if (col.gameObject.TryGetComponent<DamageReceiver>(out damageReceiver) && (!this.currentOwner || this.currentOwner.team != damageReceiver.body.team))
				{
					if (!damageReceiver.GetComponent<DamagingObject>())
					{
						this.cooldown = 0.2f;
					}
					if (damageReceiver.body.perfectBlockTime > 0f && damageReceiver.body.timeSinceLastAttacked > 0.2f)
					{
						if (!this.currentOwner || this.currentOwner.timeSinceLastAttacked < 0.22f)
						{
							if (num2 > 3f)
							{
								Sound.Play(Resources.Load<AudioClip>("Sounds/perfectBlock"), Vector2.zero, true, false, 0.5f);
								GameManager.main.SpawnPerfectBlock(col.GetContact(0).point);
								if (this.currentOwner)
								{
									this.currentOwner.hitStun += Mathf.Clamp01(this.currentOwner.hitStunMult * 0.65f);
								}
							}
							else
							{
								Sound.Play(Resources.Load<AudioClip>("Sounds/perfectBlock"), Vector2.zero, true, false, 0.2f);
							}
							col.gameObject.SendMessage("Damage", num2 * 0.5f, SendMessageOptions.DontRequireReceiver);
							if (this.currentOwner)
							{
								this.myRb.velocity = -this.myRb.velocity * 2f;
								return;
							}
						}
					}
					else
					{
						if (!this.currentOwner || string.IsNullOrEmpty(this.team) || damageReceiver.body.team != this.currentOwner.team)
						{
							damageReceiver.DoWound(this.woundType, col.GetContact(0).point);
						}
						if (this.soundOnlyOnFleshHit)
						{
							Sound.Play(this.hitSounds[Random.Range(0, this.hitSounds.Count)], base.transform.position, false, true, 1f);
						}
						damageReceiver.body.DropBlood(num2, this.woundType, col.GetContact(0).point);
						col.gameObject.SendMessage("Damage", num2, SendMessageOptions.DontRequireReceiver);
						if (this.currentOwner && damageReceiver.body.alive)
						{
							this.currentOwner.lastEngagedEnemy = damageReceiver.body;
						}
						GameManager.main.SpawnDamageNum(col.GetContact(0).point, num2 * damageReceiver.damageMult, damageReceiver.body);
						if (this.wep && ((this.woundType == 0 && num2 > 22f) || (this.woundType == 1 && num2 > 10f)))
						{
							if (this.wep.bloodQuantity <= 0f)
							{
								Color bloodColor = damageReceiver.body.bloodColor;
								bloodColor.a = this.wep.bloodQuantity;
								this.wep.blood.color = bloodColor;
							}
							else
							{
								Color color = Color.Lerp(this.wep.blood.color, damageReceiver.body.bloodColor, 0.2f);
								color.a = this.wep.bloodQuantity;
								this.wep.blood.color = color;
							}
							if (this.wep.bloodQuantity < 1f)
							{
								if (this.woundType == 0)
								{
									this.wep.bloodQuantity += 0.35f;
								}
								else
								{
									this.wep.bloodQuantity = 1f;
								}
							}
						}
						AI ai;
						if (this.currentOwner && damageReceiver.body.TryGetComponent<AI>(out ai))
						{
							ai.target = this.currentOwner;
						}
						if (this.stickOnHit && num2 > this.minStickDamage)
						{
							base.transform.position = Vector3.MoveTowards(base.transform.position, col.transform.position, 0.35f);
							this.stabbedCol = col.collider;
							this.frictionJoint = base.gameObject.AddComponent<FrictionJoint2D>();
							this.frictionJoint.maxForce = this.stabFriction;
							this.frictionJoint.maxTorque = this.stabFriction;
							this.frictionJoint.connectedBody = col.rigidbody;
							this.stabbedRb = col.rigidbody;
							this.stabCheckTime = 0.2f;
							base.GetComponent<SpriteRenderer>().sortingOrder = this.stabbedCol.GetComponent<SpriteRenderer>().sortingOrder - 2;
							this.recc = damageReceiver;
							if (num2 > 19f && damageReceiver.body.health < 0f && col.collider == damageReceiver.body.limbCols[0])
							{
								damageReceiver.body.Decapitate();
							}
						}
						if (this.isZombie && damageReceiver.body.canBeZombified && damageReceiver.body.alive && damageReceiver.body.health < 0f && this.infectionTime < 0f)
						{
							this.infectionTime = 1f;
							if (Random.Range(0f, 1f) < 0.35f)
							{
								GameManager.main.StartCoroutine("ZombifyCharacter", damageReceiver.body);
							}
						}
					}
				}
			}
		}
	}

	// Token: 0x040000B7 RID: 183
	public float minDamageSpeed;

	// Token: 0x040000B8 RID: 184
	public float damageSpeedMult;

	// Token: 0x040000B9 RID: 185
	public float damageMin;

	// Token: 0x040000BA RID: 186
	public List<AudioClip> hitSounds = new List<AudioClip>();

	// Token: 0x040000BB RID: 187
	private float cooldown;

	// Token: 0x040000BC RID: 188
	public int woundType;

	// Token: 0x040000BD RID: 189
	public bool soundOnlyOnFleshHit;

	// Token: 0x040000BE RID: 190
	public bool stickOnHit;

	// Token: 0x040000BF RID: 191
	public float minStickDamage;

	// Token: 0x040000C0 RID: 192
	public float stabFriction;

	// Token: 0x040000C1 RID: 193
	private FrictionJoint2D frictionJoint;

	// Token: 0x040000C2 RID: 194
	private WeaponScript wep;

	// Token: 0x040000C3 RID: 195
	public Collider2D stabbedCol;

	// Token: 0x040000C4 RID: 196
	private float stabCheckTime;

	// Token: 0x040000C5 RID: 197
	private Collider2D myCol;

	// Token: 0x040000C6 RID: 198
	private DamageReceiver recc;

	// Token: 0x040000C7 RID: 199
	[HideInInspector]
	public Rigidbody2D stabbedRb;

	// Token: 0x040000C8 RID: 200
	private Rigidbody2D myRb;

	// Token: 0x040000C9 RID: 201
	public List<AudioClip> pullOutSounds = new List<AudioClip>();

	// Token: 0x040000CA RID: 202
	private string team;

	// Token: 0x040000CB RID: 203
	[Tooltip("Set to the current body on all hands, and set on weapons when picked up. Makes infighting not happen.")]
	public BodyScript currentOwner;

	// Token: 0x040000CC RID: 204
	public bool isZombie;

	// Token: 0x040000CD RID: 205
	private float infectionTime;

	// Token: 0x040000CE RID: 206
	public float maxDamage = 30f;
}
